package com.jy.adscm.operation.cgxsb.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.jy.adscm.operation.cgxsb.bean.CgxsCommodityinfo;
import com.jy.adscm.operation.cgxsb.service.CgxsCommodityinfoService;
import com.jy.adscm.operation.cgxsb.service.impl.CgxsTaskinfoServiceImpl;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzf
 * @since 2019-01-10
 */
@RestController
@RequestMapping("adscm/operation/cgxsCommodityinfo")
public class CgxsCommodityinfoController {
    @Autowired
    CgxsCommodityinfoService cgxsCommodityinfoService;
    ObjectMapper mapper=new ObjectMapper();
@RequestMapping("/insert")
    public SimpleCoreResponse insert(String data){

        CgxsCommodityinfo cgxs= null;
        try {
            mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            cgxs = mapper.readValue(data, CgxsCommodityinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        cgxs.setSpid(UUID.randomUUID().toString());
        if (AdscmBaseUtils.getInstance().isNotEmpty(cgxs.getSpjhdj(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgxs.getSpjhsl(),null)){
                cgxs.setSpjhzj(cgxs.getSpjhdj()*cgxs.getSpjhsl());
            }
        }

        return new SimpleCoreResponse("200","新增成功",cgxsCommodityinfoService.insert(cgxs));
    }

    /**
     * 根据商品用途查询商品名称
     * @param type
     * @return
     */
    @RequestMapping("/queryBySpmc")
    public SimpleCoreResponse queryBySpmc(String type){
        return new SimpleCoreResponse("200","查询成功",cgxsCommodityinfoService.queryBySpmc(type));
    }
    @RequestMapping("/update")
    public SimpleCoreResponse update(String data){
        CgxsCommodityinfo cgxs= null;
        try {
            mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            cgxs = mapper.readValue(data, CgxsCommodityinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(cgxs.getSpjhdj(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgxs.getSpjhsl(),null)){
                cgxs.setSpjhzj(cgxs.getSpjhdj()*cgxs.getSpjhsl());
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(cgxs.getSpsjdj(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgxs.getSpsjsl(),null)){
                cgxs.setSpsjzj(cgxs.getSpsjdj()*cgxs.getSpsjsl());
            }
        }
        return new SimpleCoreResponse("200","修改成功",cgxsCommodityinfoService.updateById(cgxs));
    }

    @RequestMapping("/queryByTid")
    public SimpleCoreResponse queryByTid(String tid){
        return new SimpleCoreResponse("200","查询成功",cgxsCommodityinfoService.queryByTid(tid));
    }

    @RequestMapping("/querySalePro")
    public SimpleCoreResponse querySalePro(){
        return new SimpleCoreResponse("200","查询成功",cgxsCommodityinfoService.querySalePro());
    }
}

